package cn.jinjimi.app.main.widget.biz;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.Notice;
import cn.jinjimi.app.main.dialog.CommAlertDialog;

/**
 * @author zhenhua
 * @date 2018/4/26
 */

public class HomeNoticesView extends LinearLayout {
    public HomeNoticesView(Activity activity) {
        super(activity);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).
            inflate(R.layout.view_home_notices, this);
        setVisibility(GONE);

        OnClickListener l = new OnClickListener() {
            @Override
            public void onClick(View view) {
                Object tag = view.getTag();
                if (tag != null && tag instanceof  Notice) {
                    Notice notice = (Notice) tag;
                    showDialog(notice.title, notice.detail);
                }
            }
        };
        findViewById(R.id.news_1).setOnClickListener(l);
        findViewById(R.id.news_2).setOnClickListener(l);
    }

    private void showDialog(String title, String message) {
        final CommAlertDialog dialog = new CommAlertDialog(getContext(), R.style.TransparentDialog);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setRightBtn(getContext().getString(R.string.ok), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setCancelable(false);
        dialog.showDialog(CommAlertDialog.Style.ONE_BUTTON);
    }

    public void render(List<Notice> news) {
        if (news != null && news.size() > 0) {
            TextView tv1 = findViewById(R.id.news_1);
            tv1.setText(news.get(0).title);
            tv1.setTag(news.get(0));
        }
        if (news != null && news.size() > 1) {
            TextView tv2 = findViewById(R.id.news_2);
            tv2.setText(news.get(1).title);
            tv2.setTag(news.get(1));
        }

        setVisibility(VISIBLE);
    }
}
